package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;


    //@RequireLogin有注解就代表需要登陆才能操作
    @RequestMapping("")
    public String index(Model model, HttpServletRequest request) {

        //tcs
        model.addAttribute("tcs", travelCommendService.getTop5());
        //sc
        model.addAttribute("sc", travelCommendService.getTop5().get(0));


        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {

        model.addAttribute("pageInfo", travelService.query(qo));

        return "/index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo) {

        if (qo.getType() == SearchQueryObject.SEARCH_TYPE_DEST) {

            return destSearch(model, qo);
        } else if (qo.getType() == SearchQueryObject.SEARCH_TYPE_STRATEGY) {

            return strategySearch(model, qo);
        } else if (qo.getType() == SearchQueryObject.SEARCH_TYPE_TRAVEL) {

            return travelSearch(model, qo);
        } else if (qo.getType() == SearchQueryObject.SEARCH_TYPE_USER) {

            return userSearch(model, qo);
        } else {
            return allSearch(model, qo); //所有
        }
    }

    //目的地
    private String destSearch(Model model, SearchQueryObject qo) {
        SearchResultVO data = new SearchResultVO();
        //1.查询目的地对象
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        if (dest != null) {
            //2.查询目的地下的所有攻略,前5个
            long total = 0;
            List<StrategyTemplate> ss = strategySearchService.findByDestName(dest.getName());
            //查询的总数量
            total += ss.size();
            if (ss.size() > 5) {
                ss = ss.subList(0, 5);
            }
            //3.查询目的地下的所有游记,前5个
            List<TravelTemplate> ts = travelSearchService.findByDestName(dest.getName());
            //查询的总数量
            total += ts.size();
            if (ts.size() > 5) {
                ts = ts.subList(0, 5);
            }
            //4:查该目的地下的所有用户, 前5个
            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(dest.getName());
            total += us.size();
            //list集合截取前5
            if (us.size() > 5) {
                us = us.subList(0, 5);
            }
            //把多个集合设置进对象中并共享
            data.setStrategys(ss);
            data.setTotal(total);
            data.setUsers(us);
            data.setTravels(ts);

            model.addAttribute("data", data);

        }

        //是否要判断都不影响目的地的共享
        model.addAttribute("dest", dest);

        return "/index/searchDest";

    }

    //用户
    private String userSearch(Model model, SearchQueryObject qo) {

        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合

        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");


        model.addAttribute("page", page);

        return "index/searchUser";
    }

    //游记
    private String travelSearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page", page);
        return "index/searchTravel";
    }

    ///攻略
    private String strategySearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");

        model.addAttribute("page", page);
        return "index/searchStrategy";

    }

    //查询所有
    private String allSearch(Model model, SearchQueryObject qo) {
        //高亮显示方法
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");

        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        SearchResultVO data = new SearchResultVO();

        //设置需要高亮显示的内容
        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategys.getContent());
        data.setDests(dests.getContent());

        //设置查询总条数
        data.setTotal(dests.getTotalElements() + users.getTotalElements() +travels.getTotalElements() +  strategys.getTotalElements());

        //共享整个查询结果对象
        model.addAttribute("data", data);

        return "/index/searchAll";
    }
}
